home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / news / barn_201 / part01 / raw.c < prev    next >
C/C++ Source or Header  |  1991-02-07  |  2KB  |  86 lines

  1. /*
  2.  * raw.c 
  3.  *
  4.  * This is a routine for setting a given stream to raw or cooked mode on the
  5.  * Amiga . This is useful when you are using Lattice C to produce programs
  6.  * that want to read single characters with the "getch()" or "fgetc" call. 
  7.  *
  8.  * Written : 18-Jun-87 By Chuck McManis.
  9.  */
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <stdio.h>
  14.  
  15. #include <ios1.h>
  16. #include <error.h>
  17.  
  18. extern int      errno;        /* The error variable */
  19.  
  20. /*
  21.  * Function raw() - Convert the specified file pointer to 'raw' mode. This
  22.  * only works on TTY's and essentially keeps DOS from translating keys for
  23.  * you, also (BIG WIN) it means getch() will return immediately rather than
  24.  * wait for a return. You lose editing features though. 
  25.  */
  26. long
  27. raw(fp)
  28.     FILE           *fp;
  29.  
  30. {
  31.     struct MsgPort *mp;        /* The File Handle message port */
  32.     struct FileHandle *afh;
  33.     struct UFB     *ufb;
  34.     long            Arg[1], res;
  35.  
  36.     ufb = (struct UFB *) chkufb(fileno(fp));    /* Step one, get the file
  37.                          * handle */
  38.     afh = (struct FileHandle *) (ufb->ufbfh);
  39.  
  40.     if (!IsInteractive(afh)) {    /* Step two, check to see if it's a console */
  41.     errno = ENOTTY;
  42.     return (-1);
  43.     }
  44.     /* Step three, get it's message port. */
  45.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  46.     Arg[0] = -1L;
  47.     res = SendPacket(mp, ACTION_SCREEN_MODE, Arg, 1);    /* Put it in RAW: mode */
  48.     if (res == 0) {
  49.     errno = ENXIO;
  50.     return (-1);
  51.     }
  52.     return (0);
  53. }
  54.  
  55. /*
  56.  * Function - cooked() this function returns the designate file pointer to
  57.  * it's normal, wait for a <CR> mode. This is exactly like raw() except that
  58.  * it sends a 0 to the console to make it back into a CON: from a RAW: 
  59.  */
  60.  
  61. long
  62. cooked(fp)
  63.     FILE           *fp;
  64.  
  65. {
  66.     struct MsgPort *mp;        /* The File Handle message port */
  67.     struct FileHandle *afh;
  68.     struct UFB     *ufb;
  69.     long            Arg[1], res;
  70.  
  71.     ufb = (struct UFB *) chkufb(fileno(fp));
  72.     afh = (struct FileHandle *) (ufb->ufbfh);
  73.     if (!IsInteractive(afh)) {
  74.     errno = ENOTTY;
  75.     return (-1);
  76.     }
  77.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  78.     Arg[0] = 0;
  79.     res = SendPacket(mp, ACTION_SCREEN_MODE, Arg, 1);
  80.     if (res == 0) {
  81.     errno = ENXIO;
  82.     return (-1);
  83.     }
  84.     return (0);
  85. }
  86.